<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
class Shopcart extends Base{
    //加入购物车
    public function shopcartin(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.g_id"))){
            $g_id=input("param.g_id");
        }else{
            return json(['code' => 503, 'msg' => '商品id呢']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 504, 'msg' => 'openid呢']);exit();
        }
        if(!empty(input("param.parms"))){
            $parms=input("param.parms");
        }else{
            return json(['code' => 506, 'msg' => '参数呢']);exit();
        }
        if(!empty(input("param.buynum"))){
            $buynum=input("param.buynum");
        }else{
            return json(['code' => 507, 'msg' => '购买量呢']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $collect=Db::name('shopcart')->where('cus_id',$cus_id)->where('openid',$openid)->where('g_id',$g_id)->where('state',1)->find();
            if($collect){
                return json(['code' => 201, 'msg' => '您已加入此商品！']);exit();
            }else{
                if(input("param.platform")==1&&$customer['platformwx_state']==1){
                    $data['cus_id']=$cus_id;
                    $data['g_id']=$g_id;
                    $data['openid']=$openid;
                    $data['parms']=$parms;
                    $data['buynum']=$buynum;
                    $data['state']=1;
                    $data['time']=time();
                    $data['uptime']=time();
                    $enlistin=Db::name('shopcart')->insert($data);
                    if($enlistin){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败！']);exit();
                    }
                }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                    $data['cus_id']=$cus_id;
                    $data['g_id']=$g_id;
                    $data['openid']=$openid;
                    $data['parms']=$parms;
                    $data['buynum']=$buynum;
                    $data['state']=1;
                    $data['time']=time();
                    $data['uptime']=time();
                    $enlistin=Db::name('shopcart')->insert($data);
                    if($enlistin){
                        return json(['code' => 200, 'msg' => '成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败！']);exit();
                    }
                }else{
                return json(['code' => 505, 'msg' => '未开启此平台']);exit();
                }
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //取消
    public function canclecart(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 503, 'msg' => 'openid呢']);exit();
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 504, 'msg' => '相关id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $data['state']=2;
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $collect=Db::name('shopcart')->where('cus_id',$cus_id)->where('openid',$openid)->where('id',$id)->update($data);
                if($collect){
                    return json(['code' => 200, 'msg' => '成功']);exit();
                }else{
                    return json(['code' => 500, 'msg' => '失败']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $collect=Db::name('shopcart')->where('cus_id',$cus_id)->where('openid',$openid)->where('id',$id)->update($data);
                if($collect){
                    return json(['code' => 200, 'msg' => '成功']);exit();
                }else{
                    return json(['code' => 500, 'msg' => '失败']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '失败']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //我的购物车
    public function myshopcart(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 503, 'msg' => 'openid呢']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $collect=Db::name('shopcart')->alias('a')->join('shopgoods b', 'a.g_id = b.id','LEFT')->field('a.id,a.g_id,a.buynum,a.parms,b.title,b.img,b.price')->where('a.cus_id',$cus_id)->where('a.openid',$openid)->where('a.state',1)->order('a.id desc')->select();
                if($collect){
                    for ($i=0; $i <count($collect) ; $i++) { 
                        $collect[$i]['checked']=false;
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$collect]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $collect=Db::name('shopcart')->alias('a')->join('shopgoods b', 'a.g_id = b.id','LEFT')->field('a.id,a.g_id,a.buynum,a.parms,b.title,b.img,b.price')->where('a.cus_id',$cus_id)->where('a.openid',$openid)->where('a.state',1)->order('a.id desc')->select();
                if($collect){
                    for ($i=0; $i <count($collect) ; $i++) { 
                        $collect[$i]['checked']=false;
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$collect]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    public function isshopcart(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 502, 'msg' => '商品id呢']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 503, 'msg' => 'openid呢']);exit();
        }
        $collect=Db::name('shopcart')->where('cus_id',$cus_id)->where('openid',$openid)->where('g_id',$id)->where('state',1)->find();
        if($collect){
            return json(['code' => 200, 'msg' => '您已加入此商品！']);exit();
        }else{
            return json(['code' => 500, 'msg' => '未曾加入购物车！']);exit();
        }
    }
}